package org.example.controller;

import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import org.example.mapper.UserMapper;
import org.example.pojo.Result;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.utils.JwtUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/register")
    @Operation(summary = "注册", description = "注册")
    public Result register(
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="用户名")
            String username,
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="密码")
            String password){

        return userService.register(username, password);
    }

    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录")
    public Result login(
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="用户名")
            String username,
            @Pattern(regexp = "^\\S{5,16}$")
            @Parameter(description="密码")
            String password){

        return userService.login(username, password);
    }

    @GetMapping("/userInfo")
    @Operation(summary = "用户信息", description = "用户信息")
    public Result<User> userInfo(){
        return userService.userInfo();
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户", description = "更新用户")
    public  Result update(@RequestBody @Validated User user){
        return userService.update(user);
    }
    @PatchMapping("/updateAvatar")
    @Operation(summary = "更新头像", description = "更新头像")
    public  Result updateAvatar(@URL String avatarUrl){
        return userService.updateAvatar(avatarUrl);
    }

    @PatchMapping("/updatePwd")
    @Operation(summary = "修改密码", description = "修改密码")
    public  Result updatePwd(String oldPwd, String newPwd, String rePwd, @RequestHeader("Authorization") @Parameter(hidden = true) String token){

        return userService.updatePwd(oldPwd, newPwd, rePwd, token);
    }
}
